package cn.ruituo.adv8.server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TalkServer {
    public static void main(String[] args) throws IOException {

                //服务器监听在8888端口
                ServerSocket st=new ServerSocket(8880);
                System.out.println("服务启动成功！");
                while (true){
                    try {
                        Socket socket = st.accept();//和客户端建立连接
                        System.out.println(socket+"连接成功了。。。");
                        System.out.println(socket.getInetAddress()+","+socket.getPort());
                        InputStream in=socket.getInputStream();
                        OutputStream out=socket.getOutputStream();
                        BufferedReader br=new BufferedReader(new InputStreamReader(in));
                        PrintWriter pw=new PrintWriter(out,true);
                        //客户端先发消息，服务端给回复的方式
                        while(true){
                            String ask=br.readLine();// \n
                            System.out.println("收到"+socket+"发来的信息："+ask);
                            pw.println("你说的："+ask+"朕知道了");
                            if(ask.equals("exit"))
                                break;
                        }
                        socket.close();//关闭
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }



    }
}
